package com.redmoon.oa.attendance;

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;

import cn.js.fan.cache.jcs.RMCache;
import cn.js.fan.db.SQLFilter;
import cn.js.fan.util.DateUtil;
import cn.js.fan.util.ErrMsgException;
import cn.js.fan.util.StrUtil;

import com.cloudweb.oa.service.IDeptUserService;
import com.cloudweb.oa.utils.SpringUtil;
import com.cloudwebsoft.framework.util.LogUtil;
import com.redmoon.oa.flow.FormDb;
import com.redmoon.oa.visual.FormDAO;

/**
 * @Description:
 * @author:
 * @Date: 2017-9-26下午12:14:19
 */
public class ShiftScheduleMgr {
    final static String group = "ShiftScheduleMgr";

    /**
     * 默认排班记录的ID
     */
    private static long defaultScheduleId = -1;

    private static boolean defaultUseOACalendar = false;

    public static boolean isDefaultUseOACalendar() {
        return defaultUseOACalendar;
    }

    public static void refresh() {
        try {
            RMCache.getInstance().invalidateGroup(group);
        } catch (Exception e) {
            LogUtil.getLog(ShiftScheduleMgr.class).error(e.getMessage());
        }
    }

    public Vector listShift() {
        Vector v = new Vector();
        String sql = "select id from ft_shift order by id desc";
        FormDAO fdao = new FormDAO();
        try {
            v = fdao.list("shift", sql);
        } catch (ErrMsgException e) {
            LogUtil.getLog(getClass()).error(e);
        }
        return v;
    }

    /**
     * 取得用户的排班ID，通过缓存，如果没有排班，则返回默认排班
     *
     * @param userName
     * @return
     * @Description:
     */
    public static Long getScheduleId(String userName, Date dt) {
        Long fdaoId = Long.valueOf(-1);
        try {
            fdaoId = (Long) RMCache.getInstance().getFromGroup(userName, group);
        } catch (Exception e) {
            LogUtil.getLog(ShiftScheduleMgr.class).error("getSchedule:" + e.getMessage());
        }
        if (fdaoId == null) {
            // 取得用户的排班记录
            FormDAO fdao = new FormDAO();
            try {
                long defaultId = -1;
                IDeptUserService deptUserService = SpringUtil.getBean(IDeptUserService.class);
                String sql = "select id from ft_shift_schedule";
                for (FormDAO formDAO : fdao.list("shift_schedule", sql)) {
                    fdao = formDAO;

                    // 判断月份范围，按 >=begin_mounth <=end_mounth 判断
                    boolean isMonthScope = fdao.getIntValue("is_month_scope", 0) == 1;
                    if (isMonthScope) {
                        int bm = fdao.getIntValue("begin_month");
                        int em = fdao.getIntValue("end_month");
                        int m = DateUtil.getMonth(dt) + 1;
                        if (bm > m || em < m) {
                            continue;
                        }
                    }

                    if ("1".equals(fdao.getFieldValue("is_default"))) {
                        defaultId = fdao.getId();
                        defaultScheduleId = defaultId;
                        defaultUseOACalendar = "1".equals(fdao.getFieldValue("is_oa_calendar"));
                    }

                    // 判断是否属于所设置的部门
                    String depts = fdao.getFieldValue("depts");
                    String[] aryDept = StrUtil.split(depts, ",");
                    if (aryDept != null) {
                        for (String deptCode : aryDept) {
                            if (deptUserService.isUserBelongToDept(userName, deptCode)) {
                                try {
                                    RMCache.getInstance().putInGroup(userName, group, fdao.getId());
                                } catch (Exception e) {
                                    LogUtil.getLog(ShiftScheduleMgr.class).error("getScheduleId:" + e.getMessage());
                                }
                                return fdao.getId();
                            }
                        }
                    }

                    // 判断是否属于所设置的用户
                    String persons = fdao.getFieldValue("persons");
                    String[] ary = StrUtil.split(persons, ",");
                    if (ary != null) {
                        for (String person : ary) {
                            if (person.equals(userName)) {
                                try {
                                    RMCache.getInstance().putInGroup(person, group, fdao.getId());
                                } catch (Exception e) {
                                    LogUtil.getLog(ShiftScheduleMgr.class).error("getScheduleId:" + e.getMessage());
                                }
                                return fdao.getId();
                            }
                        }
                    }
                }
                if (defaultId != -1) {
                    try {
                        RMCache.getInstance().putInGroup(userName, group, defaultId);
                    } catch (Exception e) {
                        LogUtil.getLog(ShiftScheduleMgr.class).error("getScheduleId:" + e.getMessage());
                    }
                    return defaultId;
                }
            } catch (ErrMsgException e) {
                LogUtil.getLog(ShiftScheduleMgr.class).error(e);
            }
        }

        return fdaoId;
    }

    /**
     * 根据排班表及排班调整表取得用户于某天的班次记录
     *
     * @param userName
     * @param dt
     * @return
     * @Description:
     */
    public static Object[] getShiftDAO(String userName, java.util.Date dt) {
        // 是否为调整后的排班
        boolean isAdjust = false;
        // 如果排班调整表中有排班，则直接返回
        FormDAO fdao = getShiftAdjust(userName, dt);
        if (fdao != null) {
            isAdjust = true;
            // 被置为了 无班次
            if ("0".equals(fdao.getFieldValue("shift"))) {
            	fdao = null;
			}
            return new Object[]{fdao, isAdjust, false};
        }

        fdao = getShiftNormal(userName, dt);
        return new Object[]{fdao, isAdjust, true};
    }

    /**
     * 取得用户某天的正常班次，如果没有，则返回null
     *
     * @param userName
     * @param dt
     * @return
     */
    public static FormDAO getShiftNormal(String userName, java.util.Date dt) {
        Long id = getScheduleId(userName, dt);
        // 如果未排班且没有默认排班，则直接返回
        if (id == null || id == -1) {
            return null;
        }

        FormDAO fdao = new FormDAO();
        FormDb fd = new FormDb();
        fd = fd.getFormDb("shift_schedule");
        fdao = fdao.getFormDAO(id, fd);

        String repType = fdao.getFieldValue("repeat_type");
        if ("周".equals(repType)) {
            // 星期日为一周的第一天 SUN MON TUE WED THU FRI SAT
            // DAY_OF_WEEK返回值       1 	2 	3 	4 	5 	6 	7
            Calendar cal = Calendar.getInstance();
            cal.setTime(dt);
            int weekIndex = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (weekIndex == 0) {
                weekIndex = 7;
            }

            long shiftId = StrUtil.toLong(fdao.getFieldValue("shift_week" + weekIndex), -1);
            if (shiftId != -1) {
                FormDAO fdaoShift = new FormDAO();
                fd = fd.getFormDb("shift");
                return fdaoShift.getFormDAO(shiftId, fd);
            } else {
                return null;
            }
        } else {
            // 按月重复
            int day = DateUtil.getDay(dt);
            long shiftId = StrUtil.toLong(fdao.getFieldValue("shift_month" + day), -1);
            if (shiftId != -1) {
                FormDAO fdaoShift = new FormDAO();
                fd = fd.getFormDb("shift");
                return fdaoShift.getFormDAO(shiftId, fd);
            } else {
                return null;
            }
        }
    }

    /**
     * 取得用户某天被调整的班次
     *
     * @param userName
     * @param dt
     * @return
     * @Description:
     */
    public static FormDAO getShiftAdjust(String userName, java.util.Date dt) {
        String strDate = DateUtil.format(dt, "yyyy-MM-dd");
        String sql = "select id from ft_shift_adjust where user_name=" + StrUtil.sqlstr(userName) + " and mydate=" + SQLFilter.getDateStr(strDate, "yyyy-MM-dd");
        String formCode = "shift_adjust";
        FormDb fd = new FormDb();
        fd = fd.getFormDb(formCode);
        FormDAO fdao = new FormDAO(fd);
        try {
            Vector v = fdao.list(formCode, sql);
            if (v.size() > 0) {
                // 已存在，则判断值是否相等，不相等则重写
                Iterator ir = v.iterator();
                if (ir.hasNext()) {
                    fdao = (FormDAO) ir.next();
                    long shiftId = StrUtil.toLong(fdao.getFieldValue("shift"));
                    fd = fd.getFormDb("shift");
                    return fdao.getFormDAO(shiftId, fd);
                }
            }
        } catch (ErrMsgException e) {
            LogUtil.getLog(ShiftScheduleMgr.class).error(e);
        }
        return null;
    }

}
